package cn.jesin.css.service.impl;

import cn.jesin.css.entity.User;
import cn.jesin.css.mapper.UserMapper;
import cn.jesin.css.service.AdminService;
import cn.jesin.css.vo.Page;
import cn.jesin.css.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 谷汉斌
 * @description
 * @createTime 2024/8/13 上午11:17
 */
@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UserMapper userMapper;


    /**
     * 管理员查询用户信息列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @param keys     关键字
     * @return 分页信息
     */
    @Override
    public Page<UserInfo> queryUserInfoList(Integer pageNum, Integer pageSize, String keys) {
        // 页码小于等于1，则默认为1
        if (pageNum <= 1) {
            pageNum = 1;
        }
        //开始页码-1*每页显示的条数
        pageNum = (pageNum - 1) * pageSize;
        Map<String, Object> map = new HashMap<>(3);
        map.put("pageNum", pageNum);
        map.put("pageSize", pageSize);
        map.put("keys", keys);
        //查询用户信息
        List<User> userList = userMapper.selectUserInfoList(map);
        List<UserInfo> userInfoList = new ArrayList<>();
        for (User user : userList) {
            UserInfo userInfo = UserInfo.builder()
                    .address(user.getAddress())
                    .age(user.getAge())
                    .avatar(user.getAvatar())
                    .identityNum(user.getIdentityNum())
                    .nickName(user.getNickName())
                    .phone(user.getPhone())
                    .realName(user.getRealName())
                    .role(user.getRole())
                    .sex(user.getSex())
                    .username(user.getUsername())
                    .build();
            userInfoList.add(userInfo);
        }
        //查询总条数
        Integer total = userMapper.selectTotal(map);
        Page<UserInfo> page = new Page<>();
        page.setPageNum(pageNum + 1);
        page.setPageSize(pageSize);
        page.setData(userInfoList);
        page.setTotal(total);
        return page;
    }
}
